home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Imagine the Universe (9th Edition)
/
Imagine the Universe 9 - Disc 1.iso
/
pc
/
imagine
/
scripts
/
js_util.js
< prev
next >
Wrap
Text File
|
2002-05-21
|
6KB
|
269 lines
// The JavaScript Junkyard
// interesting and semi-useful JS snippets
// from scottandrew.com/junkyard/js/
// copyright 2001 scott andrew lepera
// getDistance()
// Returns the average distance between two specified points (usually for pixels)
function getDistance(x1,y1,x2,y2)
{
var d = Math.round(Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))));
if (isNaN(d)) d = 0;
return d;
}
// sortByProperty()
// Sort the array order based on a specific property of each array element,
//in forward or reverse order.
function _sortByProperty(property,rev)
{
var fn = function(a,b)
{
if (a[property] < b[property])
{
return (rev)? 1:-1;
} else if (a[property] > b[property])
{
return (rev)? -1:1;
} else
{
return 0;
}
}
this.sort(fn);
}
Array.prototype.sortByProperty = _sortByProperty;
// getAngle()
// Gives the angle of the radian described by the two specified points
function getAngle(x1,y1,x2,y2)
{
var diffH = (x1-x2);
var diffV = (y2-y1);
if (diffH){
var slope = diffV / diffH ;
var angle = Math.atan(slope);
var dgrs = (angle * 180) / Math.PI;
if (diffH < 0){dgrs += 180;}
}
else if (diffV < 0){dgrs = 270;}
else if (diffV > 0){dgrs = 90;}
else {dgrs = 0;}
if (dgrs < 0) dgrs = 360 + dgrs;
return Math.round(dgrs);
}
// roundTo()
// Round p to nearest n
function roundTo(p,n)
{
return (Math.round(n/p))*p;
}
// getRandomColor()
// Returns a random hex color. Passing true for safe returns a web safe color
function getRandomColor(safe)
{
var vals,r,n;
if (safe)
{
v = "0369cf";
n = 3;
} else
{
v = "0123456789abcdef";
n = 6;
}
var c = "#";
for (var i=0;i<n;i++)
{
var ch = v.charAt(Math.round(Math.random() * (v.length-1)));
c += (safe)?ch+ch:ch;
}
return c;
}
// addEvent and removeEvent
// cross-browser event handling for IE5+, NS6 and Mozilla
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent){
var r = elm.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be removed");
}
}
function removeEvent(elm, evType, fn, useCapture)
{
if (elm.removeEventListener){
elm.removeEventListener(evType, fn, useCapture);
return true;
} else if (elm.detachEvent){
var r = elm.detachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be removed");
}
}
// Cookie handling functions
function saveCookie(name,value,days)
{
if (days) {
var d = new Date();
d.setTime(d.getTime()+(days*24*60*60*1000));
var ex = "; expires="+date.toGMTString();
}
else {
var ex = "";
}
document.cookie = name+"="+value+ex+"; path=/";
}
function readCookie(name)
{
var eq = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i<ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(eq) == 0) return c.substring(eq.length,c.length);
}
return null;
}
function eraseCookie(name)
{
saveCookie(name,"",-1);
}
// getParams
function getQueryArgs(global)
{
var args = {};
var loc = window.location.href;
var q = loc.indexOf("?");
if (q==-1) return false;
loc = loc.substring(q+1);
var pairs = loc.split("&");
for (var i=0; i<pairs.length;i++){
if (global) eval(pairs[i]);
var keyval = pairs[i].split("=");
args[keyval[0]] = unescape(keyval[1]);
}
return args;
}
/* inPoly()
Finds if a given point is within a polygon.
Based on Bob Stein's inpoly() function for C.
http://home.earthlink.net/~bobstein/inpoly/
Modified for JavaScript by Scott Andrew LePera.
Parameters:
poly: array containing x/y coordinate pairs that
describe the vertices of the polygon. Format is
indentical to that of HTML image maps, i.e. [x1,y1,x2,y2,...]
px: the x-coordinate of the target point.
py: the y-coordinate of the target point.
Return value:
true if the point is within the polygon, false if not.
*/
function inPoly(poly,px,py)
{
var npoints = poly.length; // number of points in polygon
var xnew,ynew,xold,yold,x1,y1,x2,y2,i;
var inside=false;
if (npoints/2 < 3) { // points don't describe a polygon
return false;
}
xold=poly[npoints-2];
yold=poly[npoints-1];
for (i=0 ; i < npoints ; i=i+2) {
xnew=poly[i];
ynew=poly[i+1];
if (xnew > xold) {
x1=xold;
x2=xnew;
y1=yold;
y2=ynew;
}
else {
x1=xnew;
x2=xold;
y1=ynew;
y2=yold;
}
if ((xnew < px) == (px <= xold) && ((py-y1)*(x2-x1) < (y2-y1)*(px-x1))) {
inside=!inside;
}
xold=xnew;
yold=ynew;
}
return inside;
}
// readFile
// retrieves the contents of a local file as a JS string.
// NS4 requires Java enabled.
// Works in Mozilla 0.9.5 +
function readFile(url)
{
var req;
if (document.all){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (netscape){
if (document.getElementById){
req = new XMLHttpRequest();
}
else {
req = new NS4HttpRequest();
}
}
req.open("GET",url,false);
req.send(null);
return req.responseText;
}
function NS4HttpRequest()
{
this.url = "";
this.responseText = "";
}
NS4HttpRequest.prototype.open = function(method,url)
{
this.url = url;
this.method = method||get;
}
NS4HttpRequest.prototype.send = function()
{
// thank you Mr. Pemberton
if (this.url=="") return false;
var line,buffer;
this.responseText = "";
buffer = new java.io.BufferedReader(new java.io.InputStreamReader(new java.net.URL(this.url).openStream()));
while ((line = buffer.readLine())!=null) this.responseText+=line + "\n";
if (buffer!=null) buffer.close();
return true;
}